package de.morris;

import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.RotPosPathInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.AxisAngle4f;
import javax.vecmath.Matrix3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

public class flyingCamera extends BranchGroup {
	private Alpha a;
	private RotPosPathInterpolator interPolator;
	private TransformGroup destination;
	AxisAngle4f ax;
	Transform3D rotPos;
	float[] knots;

	public flyingCamera(TransformGroup dest) {
		destination = dest;
		createBasics();

		float[] knots = { 0.0f, 1.0f };
		Quat4f[] quats = { new Quat4f(), new Quat4f() };
		Point3f[] positions = { new Point3f(), new Point3f() };

		interPolator = new RotPosPathInterpolator(a, destination, rotPos, knots, quats, positions);

		super.addChild(interPolator);
	}

	public void createBasics() {
		rotPos = new Transform3D();
		ax = new AxisAngle4f(1.0f, 0.0f, 0.0f, 0.0f);
		rotPos.set(ax);
		a = new Alpha();
	}

	public void move(Point3f position, Matrix3f rotation, int time) {
		Transform3D td = new Transform3D();
		destination.getTransform(td);
		Matrix3f rotMatrix = new Matrix3f();
		td.getRotationScale(rotMatrix);

		AxisAngle4f angle = new AxisAngle4f();
		angle.set(rotMatrix);
		Quat4f q = new Quat4f();
		q.set(angle);
		interPolator.setQuat(0, q);
		angle = new AxisAngle4f();
		q = new Quat4f();
		angle.set(rotation);
		q.set(angle);
		interPolator.setQuat(1, q);

		Transform3D ownTransf = new Transform3D();
		destination.getTransform(ownTransf);
		Vector3f v = new Vector3f();
		ownTransf.get(v);

		interPolator.setPosition(0, new Point3f(v.x, v.y, v.z));
		interPolator.setPosition(1, position);

		a = new Alpha(1, time);
		interPolator.setAlpha(a);
		a.setStartTime(System.currentTimeMillis() - a.getTriggerTime());

		BoundingSphere bounds = new BoundingSphere(new Point3d(), 2000);
		interPolator.setSchedulingBounds(bounds);
	}

}
